package cn.javabb.project.controller;

import cn.javabb.common.model.domain.AjaxResult;
import cn.javabb.framework.web.controller.BaseController;
import cn.javabb.framework.web.page.TableDataInfo;
import cn.javabb.project.entity.GenTable;
import cn.javabb.project.entity.GenTable;
import cn.javabb.project.entity.GenTableColumn;
import cn.javabb.project.service.GenTableColumnService;
import cn.javabb.project.service.GenTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2021/05/05 01:24
 */
@Api(tags = "生成表管理")
@RestController
@RequestMapping("/table")
public class GenTableController extends BaseController {

    @Autowired
    private GenTableService genTableService;
    @Autowired
    private GenTableColumnService genTableColumnService;

    @ApiOperation("分页查询生成表")
    @GetMapping("/")
    public TableDataInfo list(GenTable table) {
        startPage();
        List<GenTable> list = genTableService.list(table);
        return getDataTable(list);
    }
    @ApiOperation("根据ID查询生成表")
    @GetMapping("/{id}")
    public AjaxResult getOne(@PathVariable String id) {
        return AjaxResult.success(genTableService.getById(id));
    }

    @ApiOperation("更新生成表")
    @PutMapping("/")
    public AjaxResult update( GenTable table) {
        table.setUpdateTime(new Date());
        if (genTableService.updateTable(table)) {
            return AjaxResult.success("更新成功");
        }
        return AjaxResult.error("更新失败");
    }
    @ApiOperation("删除生成表")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable String id) {
        if (genTableService.deleteTable(id)) {
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }
    @ApiOperation("查询所有字段")
    @GetMapping("/{id}/column")
    public TableDataInfo listByGroupId(@PathVariable("id") String id) {
        List<GenTableColumn> list = genTableColumnService.listByTableId(id);
        return getDataTable(list);
    }
    @ApiOperation("更新字段")
    @PutMapping("/column/")
    public AjaxResult updateColumn(@RequestBody GenTableColumn tableColumn) {
        tableColumn.setUpdateTime(new Date());
        if (genTableColumnService.updateById(tableColumn)) {
            return AjaxResult.success("更新成功");
        }
        return AjaxResult.error("更新失败");
    }
    @ApiOperation("删除字段")
    @DeleteMapping("/column/{id}")
    public AjaxResult deleteColumn(@PathVariable Long id) {
        if (genTableColumnService.removeById(id)) {
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }
}
